//
//  HomeCollectionView.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/9.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "ZZLayout.h"

#import "AnimRefreshHeader.h"
#import "AnimRefreshFooter.h"

#import "HomeFlowSubListViewController.h"

#import "HomeCollectionView.h"

@interface HomeCollectionView ()<
UICollectionViewDelegate,
UICollectionViewDataSource,
ZZLayoutDelegate,
XLPageViewControllerDelegate,
XLPageViewControllerDataSrouce
>


@property (nonatomic, strong) UICollectionViewCell *cell;

@property (nonatomic, strong) NSMutableArray *segArray;

@end

@implementation HomeCollectionView

- (instancetype)init {
    self = [super initWithFrame:CGRectZero];
        if (self) {

            [self addSubview:self.collectionView];
            [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.mas_equalTo(0);
            }];
            
            WEAKSELF
            [[[NSNotificationCenter defaultCenter]rac_addObserverForName:@"homeTabNoti" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
                weakSelf.canScroll = YES;
                [weakSelf subTabViewCanScroll:NO];

            }];
            self.canScroll = YES;
            
        }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
//    self.collectionView.frame = self.bounds;
}

- (void)fillWithModel:(HomeModel *)model {
    self.homeModel = model;
    
    if (self.segArray.count == 0) {
        for (int i = 0; i < self.homeModel.segDataArray.count; i++) {
            HomeFlowSubListViewController *vc = [[HomeFlowSubListViewController alloc] initWithParentId:self.homeModel.segDataArray[i].scId];
            [self.segArray addObject:vc];
        }
    }
    
    [self refreshChildVCData];
    [self.collectionView reloadData];
}

-(void)subTabViewCanScroll:(BOOL)canScroll{
    
    for (HomeFlowSubListViewController *vc in self.segArray) {
        vc.canScroll = canScroll;
        if (!canScroll) {
            vc.collectionView.contentOffset = CGPointZero;
        }
    }
}

//更新子vc的数据
- (void)refreshChildVCData {
    for (HomeFlowSubListViewController *vc in self.segArray) {
        [vc refreshChildVCData];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.tag !=10086) {
        return;
    }
    
    CGFloat naviH = self.homeModel.scrollStopHeight;
    if (scrollView.contentOffset.y>naviH) {
        scrollView.contentOffset = CGPointMake(0, naviH);
        if (self.canScroll) {
            self.canScroll = NO;
            [self subTabViewCanScroll:YES];
        }
    }else{
        if (!self.canScroll) {
            scrollView.contentOffset = CGPointMake(0, naviH);
        }
    }
}

#pragma mark- 协议方法
#pragma mark - collectionView && Layout 的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == HomeFlow_section_normal) {
        return self.homeModel.adDataArray.count;
    } else {
        return 1;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case HomeFlow_section_normal:{
            switch (self.homeModel.adDataArray[indexPath.row].tadmId) {
                case HomeFlow_itemType_bigBanner:{} break;
                case HomeFlow_itemType_gird:{} break;
                case HomeFlow_itemType_icon:{} break;
                case HomeFlow_itemType_title:{
                    [[TT_JumpManager sharedManager] jumpWithModel:self.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList.firstObject completion:^(BOOL success, NSString * _Nonnull message) {
                        
                    }];
                } break;
                case HomeFlow_itemType_YXStore:{} break;
                case HomeFlow_itemType_xBanner:{} break;
                case HomeFlow_itemType_newGird:{} break;
                case HomeFlow_itemType_newFree:{
                    if (self.HomeCollectionViewBlock) {
                        self.HomeCollectionViewBlock(HomeCollectionViewActionType_NewFree, [HomeADItemEventModel new]);
                    }
                } break;
                default: break;
            }
        } break;
        case HomeFlow_section_recommand:{} break;
        default: break;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WEAKSELF
    switch (indexPath.section) {
        case HomeFlow_section_normal:{
            switch (self.homeModel.adDataArray[indexPath.row].tadmId) {
                case HomeFlow_itemType_bigBanner:{
                    HomeFlowBannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowBannerCellID" forIndexPath:indexPath];
                    cell.bigBanner = YES;
                    NSMutableArray *photos = self.homeModel.adDataArray[indexPath.row].photos;
                    [cell configBannerCellWithImageArray:photos];
                    
                    cell.CycleImgClickedBlock = ^(NSInteger index) {
                        if (weakSelf.HomeCollectionViewBlock) {
                            weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList[index]);
                        }
                    };
                    return cell;
                    
                } break;
                case HomeFlow_itemType_gird:{
                    
                    HomeFlowServiceCatCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowServiceCatCellID" forIndexPath:indexPath];

                    [cell fillCellWithModel:self.homeModel.adDataArray[indexPath.row]];
                    
                    cell.HomeFlowServiceCatCellBlock = ^(NSInteger index) {
                    
                        HomeADItemEventModel *model = weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList[index];
                        if (weakSelf.HomeCollectionViewBlock) {
                            weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                        }
                    };
                    
                    return cell;
                    
                } break;
                case HomeFlow_itemType_mixGird:{
                    
                    HomeFlowServiceCatCell_style2  *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowServiceCatCell_style2ID" forIndexPath:indexPath];
                    [cell fillCellWithModel:self.homeModel.adDataArray[indexPath.row]];

                    __weak typeof(cell) weakCell = cell;
                    cell.HomeFlowServiceCatCell_style2Block = ^(HomeFlowServiceCatCell_style2ActionType type, NSInteger index) {
                      
                        switch (type) {
                            case HomeFlowServiceCatCell_style2ActionType_newFree:{

                            } break;
                            case HomeFlowServiceCatCell_style2ActionType_recommender:{
                                if (self.HomeCollectionViewBlock) {
                                    self.HomeCollectionViewBlock(HomeCollectionViewActionType_Recommner, [HomeADItemEventModel new]);
                                }
                            } break;
                                
                            case HomeFlowServiceCatCell_style2ActionType_getCoupon:{
                                
                                HomeADItemEventModel *model = [[HomeADItemEventModel alloc] init];
                                model.eventClickId = TT_JUMPTYPE_CouponList;
                                if (weakSelf.HomeCollectionViewBlock) {
                                    weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                                }
                            } break;
                            case HomeFlowServiceCatCell_style2ActionType_shareGift:{
                                if (self.HomeCollectionViewBlock) {
                                    self.HomeCollectionViewBlock(HomeCollectionViewActionType_PlatformEquity, [HomeADItemEventModel new]);
                                }
                            } break;
                            case HomeFlowServiceCatCell_style2ActionType_item:{
                                
                                HomeADItemEventModel *model = weakCell.dataArray[index];
                                if (weakSelf.HomeCollectionViewBlock) {
                                    weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                                }
                            } break;
                            default:
                                break;
                        }
                    };
                    
                    return cell;
                    
                } break;
                case HomeFlow_itemType_mixIcon:{
                    
                    HomeFlowStoreCatCell_style2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowStoreCatCell_style2ID" forIndexPath:indexPath];
                    [cell fillCellWithModel:self.homeModel.adDataArray[indexPath.row]];
                    
                    cell.HomeFlowStoreCatCell_style2Block = ^(NSInteger index) {

                        HomeADItemEventModel *model = weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList[index];
                        if (weakSelf.HomeCollectionViewBlock) {
                            weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                        }
                        
                    };
                    return cell;
                    
                } break;
                case HomeFlow_itemType_icon:{
                    HomeFlowStoreCatCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowStoreCatCellID" forIndexPath:indexPath];

                    [cell fillCellWithModel:self.homeModel.adDataArray[indexPath.row]];

                    cell.HomeFlowStoreCatCellBlock = ^(NSInteger index) {

                        HomeADItemEventModel *model = weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList[index];
                        if (weakSelf.HomeCollectionViewBlock) {
                            weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                        }
                        
                    };
                    return cell;
                    
                } break;
                case HomeFlow_itemType_title:{
                    HomeFlowSEHeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowSEHeadCellID" forIndexPath:indexPath];
                    [cell fillCellWithModel:self.homeModel.adDataArray[indexPath.row]];
                    return cell;
                    
                } break;
                case HomeFlow_itemType_YXStore:{
                    HomeFlowStrictElectionStoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowStrictElectionStoreCellID" forIndexPath:indexPath];

                    [cell fillCellWithModel:self.homeModel.adDataArray[indexPath.row] bottomDataArray:self.homeModel.sysAdvEvent];

                    cell.HomeFlowStrictElectionCellBlock = ^(HomeFlowStrictElectionType type, NSInteger index) {
                        switch (type) {
                            case HomeFlowStrictElectionType_top:{

                                HomeADItemEventModel *model = weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList[index];
                                if (weakSelf.HomeCollectionViewBlock) {
                                    weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                                }

                            } break;
                            case HomeFlowStrictElectionType_bottom:{

                                HomeADItemEventModel *model = weakSelf.homeModel.sysAdvEvent[index];
                                if (weakSelf.HomeCollectionViewBlock) {
                                    weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                                }

                            } break;
                            default:
                                break;
                        }
                    };
                    return cell;
                    
                } break;
                case HomeFlow_itemType_xBanner:{
                    HomeFlowBannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowBannerCellID" forIndexPath:indexPath];

                    NSMutableArray *photos = self.homeModel.adDataArray[indexPath.row].photos;
                    [cell configBannerCellWithImageArray:photos];
                    
                    cell.CycleImgClickedBlock = ^(NSInteger index) {
                        
                        HomeADItemEventModel *model = weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList[index];
                        if (weakSelf.HomeCollectionViewBlock) {
                            weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                        }
                    };
                    
                    return cell;
                    
                } break;
                case HomeFlow_itemType_newGird:{

                    HomeFlowBannerTopicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowBannerTopicCellID" forIndexPath:indexPath];

                    [cell fillCellWithModel:self.homeModel.adDataArray[indexPath.row]];
                    
                    cell.CycleImgClickedBlock = ^(NSInteger index) {
                        HomeADItemEventModel *model = weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.sysAdvEventList[index];
                        if (weakSelf.HomeCollectionViewBlock) {
                            weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                        }
                    };
                    
                    cell.HomeFlowTopicBlock = ^(HomeFlowTopicActionType type, NSInteger index) {
                      
                        switch (type) {
                            case HomeFlowTopicActionType_more:{
                                [[TT_TabbarViewController shareTabbar] setCurrentIndex:TT_TabbarIndexType_Community];
                            } break;
                            case HomeFlowTopicActionType_index:{//excList里面的东西
                                HomeADItemEventModel *model = weakSelf.homeModel.adDataArray[indexPath.row].sysAdvertisement.excList[index];
                                if (weakSelf.HomeCollectionViewBlock) {
                                    weakSelf.HomeCollectionViewBlock(HomeCollectionViewActionType_AD, model);
                                }

                            } break;
                            default:
                                break;
                        }
                    };
                    return cell;
                    
                } break;
                case HomeFlow_itemType_newFree:{
                    HomeFlowNewFreeProCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowNewFreeProCellID" forIndexPath:indexPath];
                    [cell fillCellWithModel:self.homeModel.freeNewProArray];

                    cell.HomeFlowNewFreeProCellBlock = ^{
                        if (self.HomeCollectionViewBlock) {
                            self.HomeCollectionViewBlock(HomeCollectionViewActionType_NewFree, [HomeADItemEventModel new]);
                        }
                    };
                    return cell;
                    
                } break;
                default:
                    break;
            }
        } break;
        case HomeFlow_section_recommand:{
            self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cid" forIndexPath:indexPath];
            [self.cell addSubview:self.pageViewController.view];
            [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.bottom.mas_equalTo(0);
            }];
            return self.cell;
        } break;
        default:
            break;
    }
    HomeFlowServiceCatCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowServiceCatCellID" forIndexPath:indexPath];
    return cell;
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的高
    
    switch (indexPath.section) {
        case HomeFlow_section_normal:{
            return self.homeModel.adDataArray[indexPath.row].itemHeight;
        } break;
        case HomeFlow_section_recommand:{
            return self.homeModel.recommandHeight;
        } break;
        default:
            break;
    }
    return CGFLOAT_MIN;
}

- (NSInteger)layout:(ZZLayout *)layout columnNumberAtSection:(NSInteger)section {//每个区有几列
    
    if (section == 0) {
        return 1;
    } else {
        return 1;
    }
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距
    return 0;
}

- (CGFloat)layout:(ZZLayout*)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距
    return 0;
}

- (CGFloat)layout:(ZZLayout*)layout spacingWithLastSectionForSectionAtIndex:(NSInteger)section {//本区区头和上个区区尾的间距
    return 0;
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {//设置不同分区的不同背景颜色
    return [UIColor whiteColor];
}

#pragma mark - XLPageViewController
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
        
    return self.segArray[index];
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return [NSString stringWithFormat:@"页面%ld",(long)index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.segArray.count;
}

- (XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index {
    HomeSegTitleCell *cell = [pageViewController dequeueReusableTitleViewCellWithIdentifier:@"HomeSegTitleCellID" forIndex:index];
    cell.mainTitle.text = self.homeModel.segDataArray[index].parentName;
    return cell;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {

}


-(NSMutableArray *)segArray{
    if (!_segArray) {
        _segArray = [NSMutableArray array];
    }
    return _segArray;
}

- (XLPageViewController *)pageViewController {
    
    if (!_pageViewController) {
        XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
        config.titleSelectedColor = UIColorHex(0x333333);
        config.titleNormalColor = UIColorHex(0x666666);
        config.titleSelectedFont = [UIFont boldSystemFontOfSize:15];
        config.titleNormalFont = [UIFont systemFontOfSize:14];
        config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
        config.separatorLineHidden = true;
        config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
        config.shadowLineWidth = CGFLOAT_MIN;
        config.titleSpace = CGFLOAT_MIN;
        config.titleWidth = TTScreenWidth/4;
        config.titleViewHeight = 40;

        _pageViewController = [[XLPageViewController alloc] initWithConfig:config];
        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;
        
        [_pageViewController registerNib:[UINib nibWithNibName:@"HomeSegTitleCell" bundle:nil] forTitleViewCellWithReuseIdentifier:@"HomeSegTitleCellID"];
        

    }
    return _pageViewController;
}

#pragma mark- 懒加载
- (TT_BaseCollectionView *)collectionView {
    if (!_collectionView) {
        
        ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeVertical delegate:self];
        //打开区头悬浮功能(默认关闭)
        layout.sectionHeadersPinToVisibleBounds = NO;
        _collectionView = [[TT_BaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.tag = 10086;
        
        [_collectionView registerClass:[UICollectionViewCell class]
            forCellWithReuseIdentifier:@"cid"];
        [_collectionView registerClass:[HomeFlowBannerCell class] forCellWithReuseIdentifier:@"HomeFlowBannerCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeFlowNewFreeProCell" bundle:nil] forCellWithReuseIdentifier:@"HomeFlowNewFreeProCellID"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeFlowServiceCatCell_style2" bundle:nil] forCellWithReuseIdentifier:@"HomeFlowServiceCatCell_style2ID"];

        [_collectionView registerClass:[HomeFlowServiceCatCell class] forCellWithReuseIdentifier:@"HomeFlowServiceCatCellID"];
        [_collectionView registerClass:[HomeFlowStoreCatCell class] forCellWithReuseIdentifier:@"HomeFlowStoreCatCellID"];
        [_collectionView registerClass:[HomeFlowStoreCatCell_style2 class] forCellWithReuseIdentifier:@"HomeFlowStoreCatCell_style2ID"];

        [_collectionView registerNib:[UINib nibWithNibName:@"HomeFlowBannerTopicCell" bundle:nil] forCellWithReuseIdentifier:@"HomeFlowBannerTopicCellID"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeFlowSEHeadCell" bundle:nil] forCellWithReuseIdentifier:@"HomeFlowSEHeadCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeFlowStrictElectionStoreCell" bundle:nil] forCellWithReuseIdentifier:@"HomeFlowStrictElectionStoreCellID"];

        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;

        _collectionView.bounces = YES;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}

@end
